//
//  EFWthDrawalViewController.m
//  EnjogFood
//
//  Created by guocheng on 2017/5/17.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFWthDrawalViewController.h"
#import "TPKeyboardAvoidingScrollView.h"
#import "EFConfirmView.h"
#import "iToast.h"

@interface EFWthDrawalViewController ()

@property(strong,nonatomic)UIView* topView;
@property(strong,nonatomic)UILabel* moneyLabel;
@property(strong,nonatomic)UILabel* moneyTipsLabel;
@property(strong,nonatomic)UIView* lineView;
@property(strong,nonatomic)UILabel* drawaLabel;
@property(strong,nonatomic)UITextField* drawaTF;
@property(strong,nonatomic)UIButton* confirmBtn;
@property(strong,nonatomic)UILabel* bottomTipsLabel;
@property(strong,nonatomic)EFConfirmView* confirmView;


@property (strong, nonatomic) TPKeyboardAvoidingScrollView * scrollView;

@property(strong,nonatomic)NSString* money;

@end

@implementation EFWthDrawalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"申请提现";
    }
    return self;
}

-(instancetype)initWithMoney:(NSString *)money{
    self = [self init];
    if(self){
        self.money = money;
    }
    return self;
}

-(void)initView{
    [self.view addSubview:self.scrollView];
    [self.topView addSubview:self.moneyLabel];
    [self.topView addSubview:self.moneyTipsLabel];
    [self.topView addSubview:self.lineView];
    [self.topView addSubview:self.drawaLabel];
    [self.topView addSubview:self.drawaTF];
    [self.scrollView addSubview:self.topView];
    [self.scrollView addSubview:self.confirmBtn];
    [self.scrollView addSubview:self.bottomTipsLabel];
    
    
    [self.moneyLabel setText:self.money];
    [self.moneyLabel sizeToFit];
    _moneyLabel.left = (kScreenWidth - self.moneyLabel.width)/2;
}

-(void)onClickConfirm{
    //[[UIApplication sharedApplication].keyWindow addSubview:self.confirmView];
    NSString* money = self.drawaTF.text;
    if(money.length <= 0){
        [[iToast makeText:@"提现金额不能为空"] show];
        return;
    }
    
    [APP_CONTEXT.net getDrawalWithAmount:money OnComplete:^(NSInteger code, NSDictionary *info, NSString *message) {
        if(code == 0){
            [[iToast makeText:@"提现成功"] show];
            NSString* total = self.moneyLabel.text;
            NSString* rest = [NSString stringWithFormat:@"%d",total.intValue -  money.intValue] ;
           
            [self.moneyLabel setText:rest];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [[iToast makeText:message] show];
        }
    }];
}

-(TPKeyboardAvoidingScrollView*)scrollView{
    if(_scrollView == NULL){
        _scrollView = [[TPKeyboardAvoidingScrollView alloc]initWithFrame: CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _scrollView.contentSize =  CGSizeMake(kScreenWidth, kScreenHeight - kStatusBarHeight);
    }
    return _scrollView;
}


-(UIView *)topView{
    if(_topView == NULL){
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 216)];
        _topView.backgroundColor = [UIColor whiteColor];
    }
    return _topView;
}

-(UILabel *)moneyLabel{
    if(_moneyLabel == NULL){
        _moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 47)];
        _moneyLabel.top = 46;
        _moneyLabel.font = [UIFont systemFontOfSize:53];
        _moneyLabel.textColor = kBlack333;
    }
    return _moneyLabel;
}

-(UILabel *)moneyTipsLabel{
    if(_moneyTipsLabel == NULL){
        _moneyTipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _moneyTipsLabel.top = self.moneyLabel.bottom + 10;
                _moneyTipsLabel.font = [UIFont systemFontOfSize:15];
        _moneyTipsLabel.textColor = kBlack999;
        [_moneyTipsLabel setText:@"当前可提现佣金"];
        [_moneyTipsLabel sizeToFit];
        _moneyTipsLabel.left = (kScreenWidth - _moneyTipsLabel.width)/2;;

    }
    return _moneyTipsLabel;
}

-(UIView *)lineView{
    if(_lineView == NULL){
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
        _lineView.backgroundColor = RGBCOLOR(0xECECEC);
        _lineView.top = self.moneyTipsLabel.bottom + 46;
    }
    return _lineView;
}

-(UILabel *)drawaLabel{
    if(_drawaLabel == NULL){
        _drawaLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 17)];
        _drawaLabel.top = self.lineView.bottom + 20;
        _drawaLabel.left = 20;
        _drawaLabel.font = [UIFont systemFontOfSize:15];
        _drawaLabel.textColor = kBlack333;
        [_drawaLabel setText:@"提现金额"];
    }

    return _drawaLabel;
}

-(UITextField *)drawaTF{
    if(_drawaTF == NULL){
        _drawaTF = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 17)];
        _drawaTF.textColor = kBlack999;
        _drawaTF.font = [UIFont systemFontOfSize:15];
        _drawaTF.left = kScreenWidth - _drawaTF.width - 20;
        _drawaTF.top = self.lineView.bottom + 20;
        [_drawaTF setPlaceholder:@"请输入金额"];
    }
    return _drawaTF;
}


-(UIButton *)confirmBtn{
    if(_confirmBtn == NULL){
        _confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 40, 35)];
        _confirmBtn.top = self.topView.bottom +   19;
        _confirmBtn.left = 20;
        _confirmBtn.layer.masksToBounds = YES;
        _confirmBtn.layer.cornerRadius = 6;
        _confirmBtn.backgroundColor = RGBCOLOR(0x69BA45);
        _confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _confirmBtn.titleLabel.textColor = [UIColor whiteColor];
        _confirmBtn.userInteractionEnabled = YES;
        [_confirmBtn setTitle:@"确定提现" forState:UIControlStateNormal];
        [_confirmBtn addTarget:self action:@selector(onClickConfirm) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}

-(UILabel *)bottomTipsLabel{
    if(_bottomTipsLabel == NULL){
        _bottomTipsLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 17)];
        _bottomTipsLabel.top = self.confirmBtn.bottom + 14;
        _bottomTipsLabel.font = [UIFont systemFontOfSize:12];
        _bottomTipsLabel.textColor = kBlack999;
        [_bottomTipsLabel setText:@"申请提现到账时间为每月10号"];
        [_bottomTipsLabel sizeToFit];
        _bottomTipsLabel.left = (kScreenWidth - _bottomTipsLabel.width)/2;
    }
    
    return _bottomTipsLabel;
}


-(EFConfirmView *)confirmView{
    if(_confirmView == NULL){
        _confirmView = [[EFConfirmView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) Title:@"提示" content:@"因为佣金是提现到您的微信账户中，请先关注公众号，再申请提现" block:^{
//            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
    return _confirmView;
}


@end
